package com.sgb.kjwl.view.ui.activity.pick;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.FragmentTransaction;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPickProjectBinding;
import com.sgb.kjwl.di.pick.DaggerPickProjectComponent;
import com.sgb.kjwl.di.pick.PickProjectModule;
import com.sgb.kjwl.model.entity.PickClEntity;
import com.sgb.kjwl.model.entity.PickDeviceEntity;
import com.sgb.kjwl.model.entity.PickGcEntity;
import com.sgb.kjwl.model.entity.PickLaborEntity;
import com.sgb.kjwl.model.entity.PickProjectEntity;
import com.sgb.kjwl.view.ui.fragment.pick.PickClFragment;
import com.sgb.kjwl.view.ui.fragment.pick.PickDeviceFragment;
import com.sgb.kjwl.view.ui.fragment.pick.PickGcFragment;
import com.sgb.kjwl.view.ui.fragment.pick.PickInfoFragment;
import com.sgb.kjwl.view.ui.fragment.pick.PickLaborFragment;
import com.sgb.kjwl.viewmodel.pick.PickProjectModel;
import com.swgk.core.base.di.AppComponent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import javax.inject.Inject;

@Deprecated
public class PickProjectActivity extends AppActivity {

    public static int MATERIAL = 0;
    public static int ENGINE = 1;
    public static int LABOR = 2;
    public static int MACHINE = 3;
    public static int PROJECT = 4;

    private ActivityPickProjectBinding binding;
    @Inject
    PickProjectModel viewModel;
    public AppFragment currentFragment;
    private AppFragment[] fragments = new AppFragment[5];
    private String id;

    @Override
    protected boolean statusBarDark() {
        return true;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPickProjectComponent.builder().appComponent(appComponent).pickProjectModule(new PickProjectModule(this)).build().inject(this);
    }

    public static void start(Context context, String no) {
        Intent intent = new Intent();
        intent.putExtra("no", no);
        intent.setClass(context, PickProjectActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_pick_project);
        binding.include.ivBack.setOnClickListener(this);
        binding.include.titleView.setText("项目详情");
        id = getIntent().getStringExtra("no");

        if (null == fragments[0]) {
            fragments[0] = new PickInfoFragment();
            getProjectDetail();
        }
        if (!fragments[0].isAdded()) {
            getSupportFragmentManager().beginTransaction().add(R.id.fl_content, fragments[0]).commit();
            currentFragment = this.fragments[0];
        }

        TabLayout tab = binding.tab;
        tab.addTab(tab.newTab().setText("项目介绍"));
        tab.addTab(tab.newTab().setText("材料清单"));
        tab.addTab(tab.newTab().setText("工程清单"));
        tab.addTab(tab.newTab().setText("劳务工程"));
        tab.addTab(tab.newTab().setText("机械设备"));

        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()) {
                    case 0:
                        if (null == fragments[0]) {
                            fragments[0] = new PickInfoFragment();
                            getProjectDetail();

                        }
                        addOrShowFragment(fragments[0]);
                        break;
                    case 1:
                        if (null == fragments[1]) {
                            fragments[1] = new PickClFragment();
                            getCl(false);

                        }
                        addOrShowFragment(fragments[1]);
                        break;
                    case 2:
                        if (null == fragments[2]) {
                            fragments[2] = new PickGcFragment();
                            getGc(false);

                        }
                        addOrShowFragment(fragments[2]);
                        break;
                    case 3:
                        if (null == fragments[3]) {
                            fragments[3] = new PickLaborFragment();
                            getLabor(false);

                        }
                        addOrShowFragment(fragments[3]);
                        break;

                    case 4:
                        if (null == fragments[4]) {
                            fragments[4] = new PickDeviceFragment();
                            getDevice(false);
                        }
                        addOrShowFragment(fragments[4]);
                        break;
                    default:
                        break;
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        tab.getTabAt(getIntent().getIntExtra("type", MATERIAL)).select();
    }


    protected void addOrShowFragment(AppFragment fragment) {
        if (currentFragment == fragment) {
            return;
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (!fragment.isAdded()) {
            transaction.hide(currentFragment).add(R.id.fl_content, fragment).commit();
        } else {
            transaction.hide(currentFragment).show(fragment).commit();
        }
        currentFragment = fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    public void getGc(boolean loadMore) {
        viewModel.getPickGcModel(id, loadMore);
    }

    public void showGc(List<PickGcEntity> data) {
        EventBus.getDefault().post(data);
    }

    public void getCl(boolean loadMore) {
        viewModel.getPickClModel(id, loadMore);
    }

    public void showCl(List<PickClEntity> data) {
        EventBus.getDefault().post(data);
    }

    public void getDevice(boolean loadMore) {
        viewModel.getDeviceModel(id, loadMore);
    }

    public void showDevice(List<PickDeviceEntity> data) {
        EventBus.getDefault().post(data);
    }

    public void getLabor(boolean loadMore) {
        viewModel.getLaborModel(id, loadMore);
    }

    public void showLabor(List<PickLaborEntity> data) {
        EventBus.getDefault().post(data);
    }

    public void getProjectDetail() {
        viewModel.getPickProject(id);
    }

    public void showProjectDetail(PickProjectEntity.ProjectBean data) {
        EventBus.getDefault().post(data);
    }

    public void loadNoData(String s) {
        EventBus.getDefault().post(s);
    }

    public String getId() {
        return id;
    }
}
